package com.zhiwen.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.zhiwen.common.api.Result;
import com.zhiwen.domain.dto.CollectionDTO;
import com.zhiwen.service.UserCollectionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户收藏控制器
 */
@Tag(name = "用户收藏", description = "用户收藏相关接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/collections")
public class UserCollectionController {

    private final UserCollectionService userCollectionService;

    /**
     * 添加收藏
     *
     * @param collectionDTO 收藏信息
     * @return 操作结果
     */
    @Operation(summary = "添加收藏", description = "添加内容到收藏，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "操作成功")
    @SaCheckLogin
    @PostMapping
    public Result<Boolean> addCollection(@RequestBody CollectionDTO collectionDTO) {
        boolean result = userCollectionService.addCollection(collectionDTO);
        return Result.success(result);
    }

    /**
     * 取消收藏
     *
     * @param collectionDTO 收藏信息
     * @return 操作结果
     */
    @Operation(summary = "取消收藏", description = "取消已收藏的内容，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "操作成功")
    @SaCheckLogin
    @DeleteMapping
    public Result<Boolean> cancelCollection(@RequestBody CollectionDTO collectionDTO) {
        boolean result = userCollectionService.cancelCollection(collectionDTO);
        return Result.success(result);
    }

    /**
     * 检查是否已收藏
     *
     * @param type 收藏类型
     * @param targetId 收藏目标ID
     * @return 是否已收藏
     */
    @Operation(summary = "检查是否已收藏", description = "检查当前用户是否已收藏指定内容，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "操作成功")
    @SaCheckLogin
    @GetMapping("/status")
    public Result<Boolean> checkCollected(
            @Parameter(description = "收藏类型(1-问题，2-回答)") @RequestParam Integer type,
            @Parameter(description = "收藏目标ID") @RequestParam Long targetId) {
        CollectionDTO collectionDTO = new CollectionDTO();
        collectionDTO.setType(type);
        collectionDTO.setTargetId(targetId);
        boolean collected = userCollectionService.checkCollected(collectionDTO);
        return Result.success(collected);
    }
}
